
package com.team2.shared;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.team2.cyber_eye.ActivityHelpers.HistoryItemDao;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Parcelable;
import android.util.Log;

public class HistoryItem implements Serializable {

    private static final long serialVersionUID = 9056935202104448194L;

    private static final String TAG = HistoryItem.class.getName();
    
    String id;

    String label;
    
    String imageUri;
    
    String iqeOrBarcode;

	transient Bitmap thumb;

    private void writeObject(ObjectOutputStream out) throws IOException {
        out.writeUTF(label);
        out.writeUTF(id);
        out.writeUTF(imageUri);
        out.writeUTF(iqeOrBarcode);
        if (thumb != null) {
            FileOutputStream thumbOut = HistoryItemDao.getCtx().openFileOutput(getThumbFilename(),
                    Context.MODE_PRIVATE);
            try {
                thumb.compress(CompressFormat.PNG, 80, thumbOut);
            } finally {
                thumbOut.close();
            }
        } else {
            Log.e(TAG, "thumb is null !");
        }
    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        label = in.readUTF();
        id = in.readUTF();
        imageUri = in.readUTF();
        iqeOrBarcode = in.readUTF();
        thumb = BitmapFactory.decodeFile(HistoryItemDao.getCtx().getFileStreamPath(getThumbFilename())
                .getPath());
    }

    private String getThumbFilename() throws UnsupportedEncodingException {
    
        return URLEncoder.encode("thumb_" + id + ".png", "UTF-8");
        
    }

	public Bitmap getThumb() {
		return thumb;
	}

	public void setThumb(Bitmap thumb) {
		this.thumb = thumb;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getImageUri() {
		return imageUri;
	}

	public void setImageUri(String imageUri) {
		this.imageUri = imageUri;
	}
	
	public String getIqeOrBarcode() {
		return iqeOrBarcode;
	}

	public void setIqeOrBarcode(String iqeOrBarcode) {
		this.iqeOrBarcode = iqeOrBarcode;
	}

}
